import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
import { createContext, useContext } from 'react';
import Form from '../../Form';
export var InternalFormContext = /*#__PURE__*/ createContext(undefined);
var Provider = InternalFormContext.Provider;
export { Provider };
var useForm = function useForm() {
  var _Form$useForm = Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var context = useContext(InternalFormContext);
  var fieldsShouldBindChange =
    (context === null || context === void 0 ? void 0 : context.fieldsShouldBindChange) || [];
  return [
    _objectSpread(
      _objectSpread(
        {
          // @ts-ignore
          __powerBySzzjSchemaForm: true,
        },
        form,
      ),
      {},
      {
        setFields: function setFields(flds) {
          form.setFields(flds);
          fieldsShouldBindChange.forEach(function (field) {
            var found = flds.find(function (fe) {
              return fe.name === field.name;
            });
            if (found) {
              field.onChange(found.value);
            }
          });
        },
        /**
         * 赋值联动
         * @param values
         */
        setFieldsValue: function setFieldsValue(values) {
          form.setFieldsValue(values);
          fieldsShouldBindChange.forEach(function (field) {
            if (values[field.name] !== undefined) {
              field.onChange(values[field.name]);
            }
          });
        },
      },
    ),
  ];
};
export default useForm;
